﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Express.Task;
using KZ.Utilities;

namespace KZ.Express.Presenter
{
    public class CustomerDetailPresenter : PresenterBase
    {
        private new CustomerTask _task;

        public CustomerDetailPresenter(IViewBase view)
            : base(view, new CustomerENT())
        {
            _view = view;
            _task = new CustomerTask();
        }

        public override void Save()
        {
            try
            {
                if (_view.Action == ActionName.Insert)
                {
                    if (_task.Insert(_view.Entity) > 0)
                    {
                        _view.Entity = null;
                        LoadNextSequence(Sequences.Customer);
                        _view.SetMessage(Message.BLM_SUC_INSERT.ToString(), true);
                    }
                }
                else if (_view.Action == ActionName.Update)
                {
                    if (_task.Update(_view.Entity) > 0)
                        _view.SetMessage(Message.BLM_SUC_UPDATE.ToString(), true);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }

    public class AccountDetailPresenter : PresenterBase
    {
        private new AccountTask _task;

        public AccountDetailPresenter(IViewBase view)
            : base(view, new SupplierENT())
        {
            _view = view;
            _task = new AccountTask();
        }

        public override void Save()
        {
            try
            {
                if (_view.Action == ActionName.Insert)
                {
                    if (_task.Insert(_view.Entity) > 0)
                    {
                        _view.Entity = null;
                        _view.SetMessage(Message.BLM_SUC_INSERT.ToString(), true);
                    }
                }
                else if (_view.Action == ActionName.Update)
                {
                    if (_task.Update(_view.Entity) > 0)
                        _view.SetMessage(Message.BLM_SUC_UPDATE.ToString(), true);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}